const express = require('express');
const { query } = require('../mysql/query');
const router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  res.send('用户模块');
});

router.get('/getUsersAdmin', async function (req, res, next) {
  try {
    let users= await query('SELECT * FROM ljz_meeting_clerk');
    res.send(users);
  } catch (error) {
    res.send(error);
    throw (error);
  }
});

router.get('/getUsers', async function (req, res, next) {
  try {
    let users= await query('SELECT clerk_id ,name, department FROM ljz_meeting_clerk');
    res.send(users);
  } catch (error) {
    res.send(error);
    throw (error);
  }
});

//增加一个用户
//{"username":"用户名","password":"密码","name":"对应职员名","department":"对应职员部门"}
router.post('/addUser',async function (req, res, next) {
  try {
    let {username,password,name,department} = req.body;
    await query(`INSERT INTO ljz_meeting_clerk (username,password,name,department) VALUES ('${username}','${password}','${name}','${department}')`);
    res.send('success');
  } catch (error) {
    res.send(error);
    throw (error);
  }
});

//删除一个用户
//{"clerkId":"员工号"}
router.post('/removeUser',async function (req, res, next) {
  try {
    let {clerkId} = req.body;
    await query(`DELETE FROM ljz_meeting_clerk WHERE clerk_id = ${clerkId}`);
    res.send('success');
  } catch (error) {
    res.send(error);
    throw (error);
  }
});

//修改一个用户
//{"clerkId":"员工号","username":"用户名","password":"密码","name":"对应职员名","department":"对应职员部门"}
router.post('/editUser', async function (req, res, next) {
  try {
    let {clerkId,username,password,name,department}=req.body;
    await query(`UPDATE ljz_meeting_clerk SET username = '${username}', password = '${password}',name = '${name}',department = '${department}'
    WHERE clerk_id = ${clerkId}`);
    res.send('success');
  } catch (error) {
    res.send(error);
    throw (error);
  }
})

module.exports = router;
